home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / ziplib / convert.php < prev    next >
PHP Script  |  2004-08-22  |  5KB  |  180 lines

  1. <?
  2. // *********************
  3. // * Conversion helper *
  4. // * Created by: Seven *
  5. // *********************
  6.  
  7. // Since ZIP-files like using Dostime, i've written this small helper function
  8. // set, making it easier to read and write Dostime
  9.  
  10. // clear documentation about Dostime:
  11. // www.vsft.com/hal/dostime.htm
  12. // thanks Vilma Software! :)
  13.  
  14.  
  15. // format according to MSDN:
  16. // 5 bytes for seconds divided by 2, 6 bytes for minutes and 5 bytes for the hours :)
  17.  
  18. // write 16bit dostime output
  19.  
  20. function dostime_get($offset) {
  21.  
  22.     // take about 2 kilograms of hours
  23.     $doshour = gmdate("G") + 1;
  24.     $doshour = $doshour + $offset;
  25.  
  26.         // we won't be worrying about the date, that'll be the next function ;)
  27.     if($doshour > 23) $doshour = $doshour - 24;
  28.     if($doshour < 0) $doshour = $doshour + 24;
  29.  
  30.     $doshour = $doshour * pow(2,11);
  31.  
  32.     // mix it with about 250 grams of minutes
  33.     $dosminute = gmdate("i") * pow(2,5);
  34.  
  35.     // then add a pinch of seconds
  36.     $dossecond = round(gmdate("s") / 2);
  37.  
  38.     // mix them together and whack them in the oven for about 20 minutes
  39.     $dostime = $doshour+$dosminute+$dossecond;
  40.  
  41.     // and it's ready to be served! :)
  42.     return $dostime;
  43.  
  44.     // "whack" is a registered trademark of Jamie Oliver. All rights reserved.
  45. };
  46.  
  47. // format according to MSDN:
  48. // 5 bytes for days, 4 for month, 7 for years from 1980... can go for 128 years then, so warn me in 2108 ;)
  49.  
  50. function dosdate_get($offset) {
  51.     // date
  52.     $dosyear = (gmdate("Y") - 1980);
  53.     $dosmonth = gmdate("m");
  54.     $dosday = gmdate("j");
  55.  
  56.     // checking if date is valid
  57.     // starting with... is the day too high after applying offset?
  58.     if(gmdate("G") + $offset > 23) {
  59.         $dosday++;
  60.         if($dosday > date("t")) {
  61.             $dosday = 1;
  62.             $dosmonth++;
  63.             if($dosmonth > 12) {
  64.                 $dosmonth = 1;
  65.                 $dosyear++;
  66.             };
  67.         };
  68.     };
  69.  
  70.     // then, is the day too low after applying offset?
  71.     if(gmdate("G") + 1 + $offset < 0) { // +1 to fix the erm... standard -1 offset this pc has... strange thou :p
  72.         $dosday = $dosday - 1;
  73.         if($dosday < 1) {
  74.             // ok, little helper array, containing the months that have 30 days:)
  75.             $dirtydays = array(4,6,9,11);
  76.             if(in_array($dirtydays,$dosmonth - 1)) {     // is it one month after one of the feared months, added in the array above?
  77.                 $dosday = 30;
  78.             } elseif ($dosmonth == 3) {             // is it march then?
  79.                 $dosday = 28+date("L");
  80.             } else {                    // then the month before this one must have 31 days :)
  81.                 $dosday = 31;
  82.             };
  83.  
  84.             $dosmonth --;
  85.             if($dosmonth < 1){
  86.                 $dosmonth == 12;
  87.                 $dosyear --; // i aint checking this one, we're not creating files b4 1980 anyway ;)
  88.             };
  89.         };
  90.     };
  91.  
  92.     // wow, that took me some thinking, let's go to an easier part, returning!
  93.     $dosyear = $dosyear * pow(2,9);
  94.     $dosmonth = $dosmonth * pow(2,5);
  95.     return $dosyear+$dosmonth+$dosday;
  96. }
  97.  
  98. // Now this process must be reversed aswell. I think the most easy method for this is just returning an array with data.
  99.  
  100. function dostime_return($dostime) {
  101.     $dostime = decbin(ascii2dec($dostime)); //looks nasty, but hey, it works ;)
  102.     $dostime = str_pad($dostime,16,"0",STR_PAD_LEFT);
  103.  
  104.     // retreiving the needed data... 5-6-5 was the format
  105.     // *** Warning! *** Waarschuwing! *** Achtung! ***
  106.     // I don't know if this works on little endian machines the way it works on big-endian ones
  107.     // So let's hope for the best
  108.  
  109.     $return['hours'] = substr($dostime,0,5);
  110.     $return['minutes'] = substr($dostime,5,6);
  111.     $return['seconds'] = substr($dostime,11,5);
  112.  
  113.     unset($dostime);
  114.  
  115.     // now processing the info to the right format
  116.     $return['hours'] = bindec($return['hours']);
  117.     $return['minutes'] = bindec($return['minutes']);
  118.     $return['seconds'] = bindec($return['seconds']) * 2;
  119.     return $return;
  120. }
  121.  
  122. // this is mostly a copy of dostime_return
  123. function dosdate_return($dosdate) {
  124.     $dosdate = decbin(ascii2dec($dosdate)); //looks nasty, but hey, it works ;)
  125.     $dosdate = str_pad($dosdate,16,"0",STR_PAD_LEFT);
  126.  
  127.     // retreiving the needed data... 5-4-7 was the format
  128.     // *** Warning! *** Waarschuwing! *** Achtung! ***
  129.     // I don't know if this works on little endian machines the way it works on big-endian ones
  130.     // So let's hope for the best
  131.  
  132.     $return['year'] = substr($dosdate,0,7);
  133.     $return['month'] = substr($dosdate,7,4);
  134.     $return['day'] = substr($dosdate,11,5);
  135.  
  136.     unset($dosdate);
  137.  
  138.     // now processing the info to the right format
  139.     $return['day'] = bindec($return['day']);
  140.     $return['month'] = bindec($return['month']);
  141.     $return['year'] = bindec($return['year']) + 1980;
  142.     return $return;
  143. }
  144.  
  145. // Also useful is this ascii2dec convertor, will be a well used conversion when reading a zipfile
  146. // simple but powerful :)
  147.  
  148. function ascii2dec($input) {
  149.     $end = strlen($input);
  150.     $multiplier = 1;
  151.     for($i=0; $i < $end; $i++) {
  152.         $output = $output + (ord($input[$i]) * $multiplier); // I think Max wants some credit for this [$i] method
  153.         $multiplier = $multiplier * 256;
  154.     }
  155.     unset ($input);
  156.     return $output;
  157. }
  158.  
  159. // Extension to content-type header conversion.
  160. function ext2cth($filename) {
  161.     $filename = explode(".",$filename);
  162.     $extension = array_pop($filename);
  163.  
  164.     // I kinda need a gigantic array for this, i'll do this for now by including this array and setting a little var so I know it's
  165.     // been included
  166.  
  167.     if(!$types_is_included){
  168.         require ("./array_filetypes.php");
  169.         $types_is_included = TRUE;
  170.     }
  171.  
  172.     $extension = strtolower($extension);
  173.     $filetype = $type[$extension];
  174.     if(empty($filetype)) {
  175.         $filetype = $type['default'];
  176.     }
  177.     return $filetype;    
  178. }
  179. ?>
  180.